#
# Script for migration of Maxmind GeoIP Lite to geomydns
# Scripted by Evan Park <inblaze@gmail.com>, 2007/12/27
# 
#!/bin/sh

MYSQL_USER=
MYSQL_PASSWD=
MYSQL_DBNAME=mydns
MYSQL_DATA_DIR=/opt/mysql/data
MYSQL_IMPORT=/opt/mysql/bin/mysqlimport
MYSQL=/opt/mysql/bin/mysql
GEOIP_CSV=geoip_csv.csv

### INPUT to geoip_csv table ###
cp $GEOIP_CSV $MYSQL_DATA_DIR/$GEOIP_CSV
$MYSQL_IMPORT --fields-terminated-by="," \
--fields-optionally-enclosed-by="\"" \
--lines-terminated-by="\n" \
--host=localhost \
--user=$MYSQL_USER \
--password=$MYSQL_PASSWD \
$MYSQL_DBNAME ./$GEOIP_CSV
rm $MYSQL_DATA_DIR/$GEOIP_CSV

### Migration ###
echo "Enter MySQL $MYSQL_USER's password."
$MYSQL -u$MYSQL_USER -p $MYSQL_DBNAME << EOFMYSQL
INSERT INTO geoip_ccode SELECT DISTINCT NULL, ccode, cname FROM geoip_csv;
INSERT INTO geoip_ip SELECT startnum, endnum, cidx FROM geoip_csv NATURAL JOIN geoip_ccode;
DELETE FROM geoip_csv;
EOFMYSQL

echo "Migration Done."
